// ActionScript file
import com.ufenlei.alarm.*;

import flash.display.*;
import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.containers.VBox;
import mx.managers.FocusManager;

import view.*;
import view.main.NavButton;
import view.pages.*;

[Bindable]
private var config:Config = Config.getInstance();

[Bindable]
public var alarmMgr:AlarmManager = new AlarmManager();

[Bindable]
private var rightPage:VBox;
[Bindable]
private var currentPageLabel:String;
[Bindable]
private var pages:ArrayCollection = new ArrayCollection([{'label':"设置",'content':new Setting()},
							{'label':'关于',  'content':new About()},
							{'label':'快速帮助','content':new Help()}]);

private var reminder:TimerRemindManager;
private var iconManager:IconManager;
private var fm:FocusManager;

private function init():void{
	reminder = new TimerRemindManager();
	reminder.addEventListener(RemindEvent.REMIND_CHANG,onNotifyChange);
	iconManager = new IconManager(this);
	alarmMgr.addEventListener(AlarmEvent.ALARM_TIME_UP,onAlarmTimeup);
	fm = new FocusManager(this);
	fm.setFocus(input.timeinputbox.input);
	mainnav1.addEventListener("navclick",onNavClick);
	var x:LocalDB = LocalDB.getInstance();
}
private function setPage(page:Object):void{
	if(page.label&&page.content){
		this.rightPage = page.content;
		this.currentPageLabel = page.label;
		this.currentState = "options";
	}else{
		this.currentState = '';
	}
}
/**
 * 导航事件处理函数
 **/
 private function onNavClick(event:Event):void{
 	var navbtn:NavButton = event.target as NavButton;
 	setPage(navbtn.page);
 }
/*
* 响铃事件处理
 */
private function onAlarmTimeup(evt:AlarmEvent):void{
	reminder.notify(evt.alarm);
}
private function onRefocus(event:Event):void{
	fm.setFocus(input.timeinputbox.input);
}

/**
 * 提醒改变 - 更改程序图标的tooltip
 **/
private function onNotifyChange(event:RemindEvent):void{
	if(this.reminder.hasNotify){
		this.iconManager.tooltip = this.reminder.toString();
	}else{
		this.iconManager.tooltip = this.config.appName;
	}
}

private function onClosing(evt:Event):void{
	this.visible = false;
	evt.preventDefault();
}
